#include "CommandLine.h"
#include "../algorithms/MisraGriesAlgorithm.h"
#include "StreamSource.h"
#include <iostream>

using namespace std;

CommandLine::CommandLine(char **line, int count): m_line(line), m_count(count) {
    if (m_count > 1) {
        init();
    } else {
        help();
    }
}

void CommandLine::init() {
    int pos = 1;
    int posa = 1;

    while (pos != -1) {
        if ((pos = check("-name", pos)) != -1) {

            if ((posa = checkAlgorithm(std::string("MGA"), pos + 1)) != -1) {
                MisraGriesAlgorithm<StringPiece> mga(atoi(m_line[pos + 2]));
                StreamSource *stream = new StreamSource;
                stream->feedTo(&mga);
            }
            ++pos;
        }
    }
}

int CommandLine::checkAlgorithm(std::string pattern, int pos) {
    if (pattern == *(m_line + pos)) {
        return 1;
    }
    return -1;
}


int CommandLine::check(std::string pattern, int beg) {
    for (int i = beg; i < m_count; ++i) {
        if (pattern == *(m_line + i)) {
            return i;
        }
    }
    return -1;
}

void CommandLine::help() {
    std::cout << "./scrunch -name <name algorithm> -parametr <parametr algorith>"  << std::endl;
}
